package uoc.biblioteca.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import uoc.biblioteca.mappings.object.LlibreVo;
import uoc.biblioteca.mappings.object.MilleniumVo;

public class GenerateExcelProveidor {

	private String filePath;
	private String fileName;
	
	//Columna 0 --> Titol
	//Columna 1 --> Autor
	//Columna 2 --> ISBN
	//Columna 3 --> EDITORIAL
	//Columna 4 --> EDICIO
	//Columna 5 --> NUMERO EJEMPLARES
	//Columna 6 --> PREU
	//Columna 7 --> URL
	//Columna 8 --> OBSERVACIONS
	
	private static int COLUMN_TITOL = 0;
	private static int COLUMN_AUTOR = 1;
	private static int COLUMN_ISBN = 2;
	private static int COLUMN_EDITORIAL = 3;
	private static int COLUMN_EDICIO = 4;
	private static int COLUMN_NUMEJEM = 5;
	private static int COLUMN_PREU = 6;
	private static int COLUMN_URL = 7;
	private static int COLUMN_OBSERVACIONS = 8;
	

	public void generate(MilleniumVo[] milleniumList) throws Exception  {
		
		try{
			
			File fileXls = new File(filePath,fileName);
			
			if (fileXls.delete())
				System.out.println("El fichero " + filePath+"/"+fileName + " ha sido borrado correctamente");
			else
				System.out.println("El fichero " + filePath+"/"+fileName + " no se ha podido borrar");
			
			WritableWorkbook workbook = Workbook.createWorkbook(fileXls);
			
			WritableSheet sheet = workbook.createSheet("Comandes", 0); 
			
			//Introducir encabezado.
			Label label = new Label(0, 0, "Ref");
			Label label1 = new Label(1, 0, "Num_comanda");
			Label label2 = new Label(2, 0, "ISBN");
			Label label3 = new Label(3, 0, "TITULO");
			Label label4 = new Label(4, 0, "AUTOR");
			Label label5 = new Label(5, 0, "PUBLICAC");
			Label label6 = new Label(6, 0, "EJEMPLARES");
			Label label7 = new Label(7, 0, "PRECIO EST");
			
			sheet.addCell(label);
			sheet.addCell(label1);
			sheet.addCell(label2);
			sheet.addCell(label3);
			sheet.addCell(label4);
			sheet.addCell(label5);
			sheet.addCell(label6);
			sheet.addCell(label7);
			
			
			int fila=0;
			for(int x=0;x<milleniumList.length;x++){
				MilleniumVo milleniumTemp = milleniumList[x];
				fila=x+1;
				//i = Num columna
				//j = Num fila
					
					for(int j=0;j<=7;j++){
						if(j==0){
							label = new Label(j, fila, milleniumTemp.getRef());
						}
						else if(j==1){
							label = new Label(j, fila, milleniumTemp.getNregistro());
						}
						else if(j==2){
							if(milleniumTemp.getIsbn2()!=null && !("").equals(milleniumTemp.getIsbn2())){
								label = new Label(j, fila, milleniumTemp.getIsbn2());
							}
							else{
								if(milleniumTemp.getIsbn()!=null && !("").equals(milleniumTemp.getIsbn())){
									label = new Label(j, fila, milleniumTemp.getIsbn());
								}
								else{
									label = new Label(j, fila, milleniumTemp.getIssn());
								}
							}
						}
						else if(j==3){
							label = new Label(j, fila, milleniumTemp.getTitulo());
						}
						else if(j==4){
							label = new Label(j, fila, milleniumTemp.getAutor());
						}
						else if(j==5){
							String publicacio="";
							
							if(milleniumTemp.getPublicacio()!=null && !("").equals(milleniumTemp.getPublicacio())){
								publicacio += milleniumTemp.getPublicacio();
							}
							if(milleniumTemp.getLloc()!=null && !("").equals(milleniumTemp.getLloc())){
								publicacio += milleniumTemp.getLloc();
							}
							if(milleniumTemp.getAny()!=null && !("").equals(milleniumTemp.getAny())){
								publicacio += milleniumTemp.getAny();
							}
							
							label = new Label(j, fila, publicacio);	
						}
						else if(j==6){
							label = new Label(j, fila, milleniumTemp.getEjemplares());
						}
						else if(j==7){
							label = new Label(j, fila, milleniumTemp.getPrecioEst());
						}
						
						sheet.addCell(label);
					}
					
				
				
				
			}
			

			
			
			// All sheets and cells added. Now write out the workbook
			workbook.write();
			workbook.close(); 
					
		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
		
		
	}


	public String getFilePath() {
		return filePath;
	}


	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}


	public String getFileName() {
		return fileName;
	}


	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	

	
}
